package com.mfangsoft.zhuangjialong.integration.usermanage.service.impl;




import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.app.applogin.mapper.CustomerEntityMapper;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.utils.CacheUtil;
import com.mfangsoft.zhuangjialong.common.utils.MD5Util;
import com.mfangsoft.zhuangjialong.integration.activity.mapper.BaseActivityInfoMapper;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfo;
import com.mfangsoft.zhuangjialong.integration.customer.mapper.CustomerMapper;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserInfoEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.mapper.UserRelationEntityMapper;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.model.UserInfoEntity;
import com.mfangsoft.zhuangjialong.integration.user.model.UserRelationEntity;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.BaseUserManageMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.CustomerUserManagerBasicMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.CustomerUserManagerCommunicateMapper;
//import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.CustomerCommunicateEntityMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.SellerPointEntityMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.UserCustomerReportEntityMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.UserManagerActivityTaskMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.UserManagerDeptMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.UserManagerTaskMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.mapper.UsermanagerEverydaySignMapper;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.BaseUserManage;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.BaseUserManagerModel;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.CustomerUserManagerBasic;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.CustomerUserManagerCommunicate;
//import com.mfangsoft.zhuangjialong.integration.usermanage.model.CustomerCommunicateEntity;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.SellerPointEntity;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.UserCustomerReportEntity;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.UserManagerActivityTask;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.UserManagerDept;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.UserManagerTask;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.UserManagerTaskModel;
import com.mfangsoft.zhuangjialong.integration.usermanage.model.UsermanagerEverydaySign;
import com.mfangsoft.zhuangjialong.integration.usermanage.service.UserManageService;
@Service
public class UserManageServiceImpl implements UserManageService {

	@Autowired
	private BaseUserManageMapper baseUserManageMapper;
	@Autowired
	private UserInfoEntityMapper userInfoEntityMapper;
	@Autowired
	private CustomerEntityMapper customerEntityMapper;
	@Autowired
	private UserEntityMapper userEntityMapper;
	@Autowired
	private UserRelationEntityMapper userRelationEntityMapper;
	@Autowired
	private SellerPointEntityMapper sellerPointEntityMapper;
	@Autowired
	private UserCustomerReportEntityMapper userCustomerReportEntityMapper;
	@Autowired
	private CustomerUserManagerCommunicateMapper customerUserManagerCommunicateMapper;
	@Autowired
	private UserManagerDeptMapper userManagerDeptMapper;
	@Autowired
	private BaseActivityInfoMapper baseActivityInfoMapper;
	@Autowired
	private UserManagerTaskMapper userManagerTaskMapper;
	@Autowired
	private CustomerUserManagerBasicMapper customerUserManagerBasicMapper;
	@Autowired
	private UserManagerActivityTaskMapper userManagerActivityTaskMapper;
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private UsermanagerEverydaySignMapper usermanagerEverydaySignMapper;

	
	@Override
	public void addUserManage(BaseUserManage baseUserManage) {
		baseUserManage.setCreater(UserContext.getCurrentUser().getTag().toString());
		//归属商户，如果为null，就证明是一级商户自己创建的导购
		if(baseUserManage.getShop_id() != null){
			baseUserManage.setShop_id(baseUserManage.getShop_id());
		}else{
			baseUserManage.setShop_id(UserContext.getCurrentUserId().intValue());
		}
		baseUserManage.setCreate_time(new Date());
		baseUserManage.setCreater(UserContext.getCurrentUserId().toString());
		baseUserManage.setCreater_id(UserContext.getCurrentUserId().intValue());
		baseUserManage.setUser_pwd(MD5Util.MD5(baseUserManage.getUser_pwd()));
		baseUserManageMapper.addUserManage(baseUserManage);
	}

	@Override
	public Page<Map<String,Object>> selectUserManagePage(Page<Map<String,Object>> page) {
		Integer user_level = UserContext.getCurrentUser().getUser_level();
		Long userId = UserContext.getCurrentUserId();
		List<Map<String,Object>> baseUserManagerList = new ArrayList<Map<String,Object>>();
		//存放所有的一级店铺id
		List<UserRelationEntity> shops = new ArrayList<UserRelationEntity>();
		if(user_level == 0){
			List<BaseUserManage> selectUserManageForPage = baseUserManageMapper.selectUserManageForPage(page);
			extracted(user_level, baseUserManagerList, selectUserManageForPage,userId);
			page.setData(baseUserManagerList);
			return page;
		}
		if(user_level == 3){
			Map<String, Object> map=(Map<String, Object>)page.getParam();
			Map<String,Object> m2 = new HashMap<String,Object>();
			m2.put("level", 1);//要找的等级
			m2.put("node", "node"+user_level);//自己的等级
			m2.put("user_id", userId);//自己的id
			List<UserRelationEntity> userRelationList = userRelationEntityMapper.selectUsersByOneself(m2);
			for (UserRelationEntity userRelationEntity : userRelationList) {
				shops.add(userRelationEntity);
			}
			map.put("shops", shops);
			page.setParam(map);
			List<BaseUserManage> selectUserManageForPage = baseUserManageMapper.selectUserManageForPage(page);
			extracted(user_level, baseUserManagerList, selectUserManageForPage,userId);
		}
		Map<String,Object> m = new HashMap<String,Object>();
		String node = user_level ==3?"node3":"node"+(user_level+1);
		m.put("noden", node);
		m.put("nodet", "node"+user_level);
		m.put("level", user_level);
		m.put("user_id", userId);
		Long creater_id = userRelationEntityMapper.selectParentUser(m);
		if(user_level == 2 && creater_id != 153){
			//先找到创建他的三级id
//			UserEntity userEntity3 = userEntityMapper.selectPidByUid(userId);
			Map<String, Object> map=(Map<String, Object>)page.getParam();
			Map<String,Object> m2 = new HashMap<String,Object>();
			m2.put("level", 1);//要找的等级
			m2.put("node", "node"+user_level);//自己的等级
			m2.put("user_id", userId);//自己的id
			List<UserRelationEntity> userRelationList = userRelationEntityMapper.selectUsersByOneself(m2);
			for (UserRelationEntity userRelationEntity2 : userRelationList) {
				shops.add(userRelationEntity2);
			}
			map.put("shops", shops);
			page.setParam(map);
			List<BaseUserManage> selectUserManageForPage = baseUserManageMapper.selectUserManageForPage(page);
			extracted(user_level, baseUserManagerList, selectUserManageForPage, userId);
		}
		
		if(user_level == 1){
			Map<String, Object> map=(Map<String, Object>)page.getParam();
			Map<String,Object> m2 = new HashMap<String,Object>();
			m2.put("level", 1);//要找的等级
			m2.put("node", "node"+user_level);//自己的等级
			m2.put("user_id", userId);//自己的id
			List<UserRelationEntity> userRelationList = userRelationEntityMapper.selectUsersByOneself(m2);
			for (UserRelationEntity userRelationEntity : userRelationList) {
				shops.add(userRelationEntity);
			}
			map.put("shops", shops);
			page.setParam(map);
			List<BaseUserManage> selectUserManageForPage =  baseUserManageMapper.selectUserManageForPage(page);
			extracted(user_level, baseUserManagerList, selectUserManageForPage, userId);
		}
		
		page.setData(baseUserManagerList);
		return page;
	}

	private void extracted(Integer user_level, List<Map<String,Object>> baseUserManagerList,
			List<BaseUserManage> selectUserManageForPage,Long userId) {
		for (BaseUserManage baseUserManage : selectUserManageForPage) {
			Map<String,Object> baseUserManagerModel = new HashMap<String,Object>();
			baseUserManagerModel.put("id", baseUserManage.getId());
			baseUserManagerModel.put("name", baseUserManage.getName());
			baseUserManagerModel.put("phone", baseUserManage.getPhone());
			baseUserManagerModel.put("creater_name", (userInfoEntityMapper.selectCreaterByUserId(baseUserManage.getCreater_id().longValue()))==null?"":(userInfoEntityMapper.selectCreaterByUserId(baseUserManage.getCreater_id().longValue())).getName());
			baseUserManagerModel.put("shop_id", baseUserManage.getShop_id());
			baseUserManagerModel.put("sys_user_name", (userInfoEntityMapper.selectCreaterByUserId(baseUserManage.getShop_id().longValue()))==null?"":(userInfoEntityMapper.selectCreaterByUserId(baseUserManage.getShop_id().longValue())).getName());
			baseUserManagerModel.put("user_name", baseUserManage.getUser_name());
			//设置导购的推广数量
			baseUserManagerModel.put("generalize_num", customerEntityMapper.selectGeneralize_numByC_id(baseUserManage.getId()));
			baseUserManagerModel.put("create_time", baseUserManage.getCreate_time());
			baseUserManagerModel.put("update_time", baseUserManage.getUpdate_time());
			//设置导购的类型
			baseUserManagerModel.put("section_name", userManagerDeptMapper.selectByPrimaryKey(baseUserManage.getSection_id().longValue())==null?"":userManagerDeptMapper.selectByPrimaryKey(baseUserManage.getSection_id().longValue()).getDept_name());
			//1表示正常，0表示停用
			baseUserManagerModel.put("status", baseUserManage.getStatus()==1?"正常":"关闭");
			baseUserManagerModel.put("create", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(baseUserManage.getCreate_time()));
			baseUserManagerModel.put("update", baseUserManage.getUpdate_time() == null ? "暂无修改" : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(baseUserManage.getUpdate_time()));
			baseUserManagerModel.put("zzh", baseUserManage.getZzh() == null ? "" : baseUserManage.getZzh());
			baseUserManagerModel.put("bmh", baseUserManage.getBmh() == null ? "" : baseUserManage.getBmh());
			if(user_level == 0){
				baseUserManagerModel.put("isEditer", false);
			}else if(baseUserManage.getCreater_id() == userId.intValue()){
				baseUserManagerModel.put("isEditer", true);
			}else if(user_level == 1){
				baseUserManagerModel.put("isEditer", true);
			}else{
				baseUserManagerModel.put("isEditer", false);
			}
			baseUserManagerModel.put("user_level", user_level);
			baseUserManagerList.add(baseUserManagerModel);
		}
	}

	@Override
	public BaseUserManagerModel selectUserManageOne(int id) {
		BaseUserManagerModel userManage = baseUserManageMapper.selectUserManageOne(id);
		userManage.setUser_pwd("******");
		return userManage;
	}

	@Override
	public void updateUserManageOne(BaseUserManage baseUserManage) {
		baseUserManage.setUpdate_time(new Date());
		baseUserManage.setModify_id(UserContext.getCurrentUserId().intValue());
		baseUserManageMapper.updateUserManageOne(baseUserManage);
	}

	@Override
	public Page<SellerPointEntity> selectIntegralsByUserManagerId(Page<SellerPointEntity> page) {
		List<SellerPointEntity> sellerPointList = sellerPointEntityMapper.selectIntegralsByUserManagerIdForPage(page);
		page.setData(sellerPointList);
		return page;
	}

	@Override
	public Page<UserCustomerReportEntity> selectCustomerReportByUserManagerId(Page<UserCustomerReportEntity> page) {
		List<UserCustomerReportEntity> userCustomerReportEntityList = userCustomerReportEntityMapper.selectCustomerByUserManagerIdForPage(page);
		page.setData(userCustomerReportEntityList);
		return page;
	}

	@Override
	public CustomerUserManagerCommunicate selectRelatedCustomerCommunicateForPage(Map<String,Object> map) {
		CustomerUserManagerCommunicate customerCommunicate = customerUserManagerCommunicateMapper.selectRelatedCustomerCommunicate(map);
		return customerCommunicate;
	}


	@Override
	public Map<String, Object> selectStairShopsByUid(String code) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", code);
		map.put("noden", "node"+UserContext.getCurrentUser().getUser_level());
		map.put("level", UserContext.getCurrentUser().getUser_level());
		map.put("user_id", UserContext.getCurrentUserId());
		List<UserInfoEntity> userInfoEntityList = userInfoEntityMapper.selectStairShopsByUid(map);
		Map map1 = new HashMap<>();
		for (UserInfoEntity userInfoEntity : userInfoEntityList) {
			map1.put("shop_"+userInfoEntity.getSys_user_id(), userInfoEntity.getName());
		}
		return map1;
	}

	@Override
	public void addUserManageDept(UserManagerDept userManagerDept) {
		userManagerDept.setCreate_time(new Date());
		userManagerDept.setUpdate_time(userManagerDept.getCreate_time());
		userManagerDept.setType(userManagerDeptMapper.selectMaxId().intValue()+1);
		userManagerDeptMapper.insertSelective(userManagerDept);
	}

	@Override
	public void updateUserManageDept(UserManagerDept userManagerDept) {
		userManagerDept.setUpdate_time(new Date());
		userManagerDeptMapper.updateByPrimaryKeySelective(userManagerDept);
	}

	@Override
	public List<UserManagerDept> selectUserManageDepts() {
		return userManagerDeptMapper.selectUserManageDepts();
	}

	@Override
	public List<BaseActivityInfo> getActivitys(Integer num) {
		Long user_id = UserContext.getCurrentUserId();
		return baseActivityInfoMapper.getActivitysByUserId(user_id,num);
	}

	@Override
	public void addUerManagerTask(UserManagerTask userManagerTask) {
		userManagerTask.setShop_id(UserContext.getCurrentUserId());
		userManagerTask.setSet_time(new Date());
		userManagerTask.setUpdate_time(userManagerTask.getSet_time());
		userManagerTask.setInvite_activity_num(0);//已成单数
		userManagerTask.setInvite_customer_num(0);//已邀请客户数
		userManagerTaskMapper.insertSelective(userManagerTask);
	}

	@Override
	public void addUerManagerTaskBatch(Map<String, Object> map) throws Exception  {
		Long activity_id = Long.parseLong(map.get("activity_id").toString());
		Map map1 = (Map) map.get("user_userManager_tree");
		List<Integer> shopIds = (List<Integer>) map1.get("shop_id");
		List<Integer> ids = (List<Integer>) map1.get("ids");
		UserManagerActivityTask userManagerActivityTask = new UserManagerActivityTask();
		userManagerActivityTask.setActivity_id(activity_id);
		userManagerActivityTask.setCreate_time(new Date());
		userManagerActivityTask.setUpdate_time(userManagerActivityTask.getCreate_time());
		userManagerActivityTask.setOrder_num(Integer.parseInt(map.get("activity_num").toString()));
		userManagerActivityTask.setSign_rate(Double.parseDouble(map.get("sign_rate").toString()));
		Date task_starttime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("task_starttime").toString());
		userManagerActivityTask.setTask_starttime(task_starttime);
		Date task_endtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("task_endtime").toString());
		userManagerActivityTask.setTask_endtime(task_endtime);
		userManagerActivityTaskMapper.insertSelective(userManagerActivityTask);
		for (Integer shopId : shopIds) {
			userManagerActivityTaskMapper.insertUsermanagerActivityTaskShopRelation(shopId.longValue(),userManagerActivityTask.getId());
		}
		for (Integer userManager_id : ids) {
			UserManagerTask userManagerTask = new UserManagerTask();
			userManagerTask.setActivity_id(activity_id);
			userManagerTask.setTask_starttime(userManagerActivityTask.getTask_starttime());
			userManagerTask.setTask_endtime(userManagerActivityTask.getTask_endtime());
			userManagerTask.setUser_manager_id(userManager_id.longValue());
			userManagerTask.setShop_id(baseUserManageMapper.selectByPrimaryKey((Integer)userManager_id)==null ? null : baseUserManageMapper.selectByPrimaryKey((Integer)userManager_id).getShop_id().longValue());
			userManagerTask.setActivity_num(Integer.parseInt(map.get("activity_num").toString()));
			userManagerTask.setSet_time(userManagerActivityTask.getCreate_time());
			userManagerTask.setUpdate_time(userManagerTask.getSet_time());
			userManagerTask.setSign_rate(Double.parseDouble(map.get("sign_rate").toString()));
			userManagerTask.setActivity_task_id(userManagerActivityTask.getId());
			userManagerTaskMapper.insertSelective(userManagerTask);
		}
		
	}

	@Override
	public Page<UserManagerTaskModel> selectUerManagerTask(Page<UserManagerTaskModel> page) {
		List<UserManagerTaskModel> selectUerManagerTask = userManagerTaskMapper.selectUerManagerTaskForPage(page);
		page.setData(selectUerManagerTask);
		return page;
	}

	@Override
	public UserManagerTaskModel selectUerManagerTaskDetails(Long task_id,Long userManager_id) {
		return userManagerTaskMapper.selectUerManagerTaskDetails(task_id,userManager_id);
	}

	@Override
	public void updateUerManagerTask(UserManagerTask userManagerTask) {
		userManagerTask.setUpdate_time(new Date());
		userManagerTaskMapper.updateByPrimaryKeySelective(userManagerTask);
	}

	@Override
	public Page<UserManagerTaskModel> selectPresentUerManagerTask(Page<UserManagerTaskModel> page) {
		List<UserManagerTaskModel> selectUerManagerTask = userManagerTaskMapper.selectPresentUerManagerTaskForPage(page);
		page.setData(selectUerManagerTask);
		return page;
	}

	@Override
	public void addCommunicate(CustomerUserManagerCommunicate customerCommunicateEntity) {
//		Long lastState = customerUserManagerCommunicateMapper.selectLastState(customerCommunicateEntity.getCustomer_id(),customerCommunicateEntity.getUserManager_id());
//		if(lastState != null && lastState == 9){
//			return;
//		}
		CustomerUserManagerCommunicate c = customerUserManagerCommunicateMapper.selectCustomerUserManagerCommunicate(customerCommunicateEntity);
		if(c == null){
			customerCommunicateEntity.setCreate_time(new Date());
			customerCommunicateEntity.setUpdate_time(customerCommunicateEntity.getCreate_time());
			customerCommunicateEntity.setLastState(customerCommunicateEntity.getCustomerUserManagerBasic().getState());
			customerUserManagerCommunicateMapper.insertSelective(customerCommunicateEntity);
		}else{
			customerCommunicateEntity.setCommunicate_id(c.getCommunicate_id());
			customerCommunicateEntity.setUpdate_time(new Date());
			customerCommunicateEntity.setLastState(customerCommunicateEntity.getCustomerUserManagerBasic().getState());
			customerUserManagerCommunicateMapper.updateByPrimaryKeySelective1(customerCommunicateEntity);
		}
		CustomerUserManagerBasic customerUserManagerBasic = customerCommunicateEntity.getCustomerUserManagerBasic();
		customerUserManagerBasic.setCommunicate_id(customerCommunicateEntity.getCommunicate_id());
		customerUserManagerBasic.setCommunicate_time(customerCommunicateEntity.getCommunicate_time_start());
		customerUserManagerBasic.setCreate_time(new Date());
		customerUserManagerBasic.setUpdate_time(customerUserManagerBasic.getCreate_time());
		customerUserManagerBasicMapper.insertSelective(customerUserManagerBasic);
		//沟通记录积分的增加
		int num = sellerPointEntityMapper.selectIsFirstAddByUserManagerId(customerCommunicateEntity.getUserManager_id(),5);
		SellerPointEntity sellerPointEntity = new SellerPointEntity();
		sellerPointEntity.setSeller_id(customerCommunicateEntity.getUserManager_id());
		sellerPointEntity.setType_index(5);
		sellerPointEntity.setType_name("沟通");
		sellerPointEntity.setTitle("沟通积分");
		sellerPointEntity.setCreate_time(customerCommunicateEntity.getCreate_time());
		if(num == 0){
			sellerPointEntity.setPoint(16);
		}else{
			sellerPointEntity.setPoint(8);
		}
		sellerPointEntityMapper.insertSelective(sellerPointEntity);
	}

	@Override
	public void addCustomerReport(UserCustomerReportEntity userCustomerReportEntity) {
		userCustomerReportEntity.setCreate_time(new Date());
		userCustomerReportEntityMapper.insertSelective(userCustomerReportEntity);
	}

	@Override
	public BaseUserManage selectExclusiveConsultant(Map<String, Object> map) {
		Integer userManager_id =  baseUserManageMapper.selectExclusiveConsultant(map);
		BaseUserManage baseUserManage = baseUserManageMapper.selectByPrimaryKey(userManager_id);
		baseUserManage.setWeixinhao(baseUserManage.getWeixinhao() == null? "无":baseUserManage.getWeixinhao());
		return baseUserManage;
	}

	@Override
	public List<BaseUserManage> selectAllUserManager(Map<String, Object> map) {
//		map.put("shop_id", UserContext.getCurrentUserId());
		List<BaseUserManage> baseUserManageList = baseUserManageMapper.selectAllUserManager(map);
		return baseUserManageList;
	}

	@Override
	public void addIntegralsByUserManagerId(SellerPointEntity sellerPointEntity) {
		sellerPointEntity.setCreate_time(new Date());
		sellerPointEntityMapper.insertSelective(sellerPointEntity);
	}

	@Override
	public void updateCustomerUserManager(Map<String, Object> map) {
		map.put("shop_id", UserContext.getCurrentUserId());
		baseUserManageMapper.updateCustomerUserManager(map);
		Map<String,Object> map1 =  new HashMap<String,Object>();
		map1.put("sellerId", map.get("user_manager_id"));
		map1.put("customerId", map.get("customer_id"));
		customerMapper.updateCustomerOrderSeller(map1);
	}

	@Override
	public void updateCustomerUserManagerBath(Map<String, Object> map) {
		map.put("shop_id", UserContext.getCurrentUserId());
		baseUserManageMapper.updateCustomerUserManagerBath(map);     
		List<Integer> customer_ids = (List<Integer>) map.get("customer_ids");
		for (Integer customer_id : customer_ids) {
			Map<String,Object> map1 =  new HashMap<String,Object>();
			map1.put("sellerId", map.get("user_manager_id"));
			map1.put("customerId", customer_id);
			customerMapper.updateCustomerOrderSeller(map1);
		}
	}

	@Override
	public List<CustomerUserManagerBasic> selectRecordOfCommunication(Map<String, Object> map) {
		return customerUserManagerBasicMapper.selectRecordOfCommunication(map);
	}

	@Override
	public CustomerUserManagerCommunicate selectCommunicate(CustomerUserManagerCommunicate customerCommunicateEntity) {
		CustomerUserManagerCommunicate selectCustomerUserManagerCommunicate = customerUserManagerCommunicateMapper.selectCustomerUserManagerCommunicate(customerCommunicateEntity);
		if(selectCustomerUserManagerCommunicate != null){
//			Integer lastState = selectCustomerUserManagerCommunicate.getLastState();
//			if(lastState >= 9){
//				selectCustomerUserManagerCommunicate.setLastState(9);
//			}else{
//				selectCustomerUserManagerCommunicate.setLastState(lastState+=1);
//			}
		}
		return selectCustomerUserManagerCommunicate;
	}

	@Override
	public BaseActivityInfo selectActivityTime(Integer activity_id) {
		return baseActivityInfoMapper.selectByPrimaryKey(activity_id);
	}

	@Override
	public void updateUserManagePwd(BaseUserManage baseUserManage) {
		baseUserManage.setUser_pwd(MD5Util.MD5(baseUserManage.getUser_pwd()));
		baseUserManageMapper.updateByPrimaryKeySelective(baseUserManage);
	}

	@Override
	public BaseUserManage selectUserManageName(String user_name) {
		return baseUserManageMapper.selectUserManageName(user_name);
	}

	/**
	 * 导购增加积分
	 * @param seller_id    导购id     Long
	 * @param type_index    积分类型1.分享2.订单3.邀请    Integer
	 * @param type_name     String
	 * @param point    积分数量     Integers
	 * @param title       如果积分类型是1，title就是活动名称，积分类型是2，title就是订单号，积分类型是3，title就是"邀请积分"写死           String
	 */
	@Override
	public void addIntegral(SellerPointEntity sellerPointEntity) {
		sellerPointEntity.setCreate_time(new Date());
		int num = sellerPointEntityMapper.selectIsFirstShareBySellerId(sellerPointEntity.getSeller_id());
		if(num == 0){
			sellerPointEntity.setPoint(sellerPointEntity.getPoint()*2);
		}
		sellerPointEntityMapper.insertSelective(sellerPointEntity);
	}

	@Override
	public Integer selectUserManagerIntegral(Long userManagerId) {
		return sellerPointEntityMapper.selectUserManagerIntegral(userManagerId);
	}

	@Override
	public Page<SellerPointEntity> selectUserManagerIntegralManagement(Page<SellerPointEntity> page) {
		List<SellerPointEntity> list = sellerPointEntityMapper.selectUserManagerIntegralManagementForPage(page);
		page.setData(list);
		return page;
	}

	@Override
	public Map<String, Object> selectUerManagerTaskHint(Map<String, Object> map) {
		Long activity_id = Long.parseLong(map.get("activity_id").toString());
		@SuppressWarnings("unchecked")
		ArrayList<Integer> ids = (ArrayList<Integer>)map.get("ids");
		List<String> cunzai = new ArrayList<String>();
		List<String> bucunzai = new ArrayList<String>();
		for (Integer userManagerId : ids) {
			//TODO
			UserManagerTask userManagerTask = userManagerTaskMapper.selectTaskByUserManagerId(userManagerId,activity_id);
			String name = baseUserManageMapper.selectByPrimaryKey(userManagerId.intValue()).getName();
			if(userManagerTask == null){
				bucunzai.add(name);
			}else{
				cunzai.add(name);
			}
		}
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("cunzai", cunzai);
		m.put("bucunzai", bucunzai);
		return m;
	}

	@Override
	public Page<Map<String, Object>> selectUserActivity(Page<Map<String, Object>> page) {
		Map<String, Object> map=(Map<String, Object>)page.getParam();
		map.put("shop_id", UserContext.getCurrentUserId());
		page.setParam(map);
		List<Map<String,Object>> activitys = baseUserManageMapper.selectUserActivityForPage(page);
		page.setData(activitys);
		return page;
	}

	@Override
	public List<Map<String, Object>> selectUserManagerTaskTree(Map<String, Object> map) {
		Long activity_id = Long.parseLong(map.get("activity_id").toString());
		map.put("user_id", UserContext.getCurrentUserId());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> userInfoList = baseUserManageMapper.selectUserInfo(map);
		for (Map<String, Object> userMap : userInfoList) {
			Long shop_id = Long.parseLong(userMap.get("id").toString());
			Integer unSetTaskUserManagerNum = baseUserManageMapper.selectUnSetTaskUserManagerNum(shop_id,activity_id);
			if(unSetTaskUserManagerNum == 0){
				
			}else{
				list.add(userMap);
			}
		}
//		list.addAll(userInfoList);
		for (Map<String, Object> userInfo : userInfoList) {
			Long userId = Long.parseLong(userInfo.get("id").toString());
			map.put("userId",userId);
			List<Map<String,Object>> userManagerList = baseUserManageMapper.selectUserManagerByActivityId(map);
			list.addAll(userManagerList);
		}
		return list;
	}

	@Override
	public Page<Map<String, Object>> selectUserManagerActivityTaskList(Page<Map<String, Object>> page) {
		List<Map<String,Object>> list = baseUserManageMapper.selectUserManagerActivityTaskListForPage(page);
		page.setData(list);
		return page;
	}

	@Override
	public Map<String, Object> selectUserManagerActivityTaskDetails(Long activityTaskId) {
		UserManagerActivityTask userManagerActivityTask = userManagerActivityTaskMapper.selectByPrimaryKey(activityTaskId);
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("task_starttime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(userManagerActivityTask.getTask_starttime()));
	    map.put("task_endtime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(userManagerActivityTask.getTask_endtime()));
	    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
	    List<Map<String,Object>> userList = userManagerActivityTaskMapper.selectUserTreeByActivityTaskId(activityTaskId);
	    List<Map<String,Object>> userManagerList = userManagerActivityTaskMapper.selectUserManagerTreeByActivityTaskId(activityTaskId);
	    list.addAll(userList);
	    list.addAll(userManagerList);
	    map.put("user_userManager_tree", list);
	    map.put("activity_num", userManagerActivityTask.getOrder_num());
	    map.put("sign_rate", userManagerActivityTask.getSign_rate());
		return map;
	}

	@Override
	public void updateUserManagerActivityTask(Map<String, Object> map) throws Exception {
		Long activityTaskId = Long.parseLong(map.get("activityTaskId").toString());
		UserManagerActivityTask userManagerActivityTask = new UserManagerActivityTask();
		userManagerActivityTask.setId(activityTaskId);
		userManagerActivityTask.setTask_starttime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("task_starttime").toString()));
		userManagerActivityTask.setTask_endtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(map.get("task_endtime").toString()));
		userManagerActivityTask.setOrder_num(Integer.parseInt(map.get("activity_num").toString()));
		userManagerActivityTask.setSign_rate(Double.parseDouble(map.get("sign_rate").toString()));
		userManagerActivityTask.setUpdate_time(new Date());
		userManagerActivityTaskMapper.updateByPrimaryKeySelective(userManagerActivityTask);
		userManagerActivityTaskMapper.deleteRelationByActivityTaskId(activityTaskId);
		userManagerActivityTaskMapper.deleteUserManagerTaskByActivityTaskId(activityTaskId);
		Long activity_id = userManagerActivityTaskMapper.selectByPrimaryKey(activityTaskId).getActivity_id();
		Map map1 = (Map) map.get("user_userManager_tree");
		List<Integer> shopIds = (List<Integer>) map1.get("shop_id");
		List<Integer> ids = (List<Integer>) map1.get("ids");
		for (Integer shopId : shopIds) {
			userManagerActivityTaskMapper.insertUsermanagerActivityTaskShopRelation(shopId.longValue(),activityTaskId);
		}
		for (Integer userManager_id : ids) {
			UserManagerTask userManagerTask = new UserManagerTask();
			userManagerTask.setActivity_id(activity_id);
			userManagerTask.setTask_starttime(userManagerActivityTask.getTask_starttime());
			userManagerTask.setTask_endtime(userManagerActivityTask.getTask_endtime());
			userManagerTask.setUser_manager_id(userManager_id.longValue());
			userManagerTask.setShop_id(baseUserManageMapper.selectByPrimaryKey((Integer)userManager_id)==null ? null : baseUserManageMapper.selectByPrimaryKey((Integer)userManager_id).getShop_id().longValue());
			userManagerTask.setActivity_num(Integer.parseInt(map.get("activity_num").toString()));
			userManagerTask.setSet_time(userManagerActivityTask.getUpdate_time());
			userManagerTask.setUpdate_time(userManagerTask.getSet_time());
			userManagerTask.setSign_rate(Double.parseDouble(map.get("sign_rate").toString()));
			userManagerTask.setActivity_task_id(activityTaskId);
			userManagerTaskMapper.insertSelective(userManagerTask);
		}
	}

	@Override
	public Map<String, Object> selectUserManagerActivityTaskEcho(Long activityTaskId) {
		UserManagerActivityTask userManagerActivityTask = userManagerActivityTaskMapper.selectByPrimaryKey(activityTaskId);
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("task_starttime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(userManagerActivityTask.getTask_starttime()));
	    map.put("task_endtime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(userManagerActivityTask.getTask_endtime()));
	    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
	    //查看选中的商铺
	    List<Map<String,Object>> userListChecked = userManagerActivityTaskMapper.selectUserTreeByActivityTaskId1(activityTaskId);
	    //查看选中的导购
	    List<Map<String,Object>> userManagerListChecked = userManagerActivityTaskMapper.selectUserManagerTreeByActivityTaskId1(activityTaskId);
	    Map<String,Object> map1 = new HashMap<String,Object>();
	    map1.put("user_id", UserContext.getCurrentUserId());
	    map1.put("activity_id", userManagerActivityTask.getActivity_id());
	    //查看所有参与该活动的店铺
	    List<Map<String, Object>> userInfoList = baseUserManageMapper.selectUserInfo(map1);
		for (Map<String, Object> map2 : userInfoList) {
			map2.put("checked", "false");
			for (Map<String, Object> map3 : userListChecked) {
				if(Integer.parseInt(map2.get("id").toString()) == Integer.parseInt(map3.get("id").toString())){
					map2.put("checked", "true");
					map2.put("open", "true");
				}
			}
			Long shop_id = Long.parseLong(map2.get("id").toString());
			Integer unSetTaskUserManagerNum = baseUserManageMapper.selectUnSetTaskUserManagerNum(shop_id,userManagerActivityTask.getActivity_id());
			if(map2.get("checked").toString().equals("true")){
				list.add(map2);
			}
			if(map2.get("checked").toString().equals("false") && unSetTaskUserManagerNum == 0){
				
			}
			if(map2.get("checked").toString().equals("false") && unSetTaskUserManagerNum != 0){
				list.add(map2);
			}
		}
		//查看未选中的导购
		List<Map<String,Object>> userManagerListUnChecked = userManagerTaskMapper.selectUserManagerByActivityIdAndUserId(userManagerActivityTask.getActivity_id(),UserContext.getCurrentUserId());
		list.addAll(userManagerListChecked);
		list.addAll(userManagerListUnChecked);
	    map.put("user_userManager_tree", list);
	    map.put("activity_num", userManagerActivityTask.getOrder_num());
	    map.put("sign_rate", userManagerActivityTask.getSign_rate());
		return map;
	}

	@Override
	public Boolean selectUserManagerIsSing(Long userManagerId) throws Exception {
		long currentTimeMillis = System.currentTimeMillis();
		UsermanagerEverydaySign usermanagerSign = usermanagerEverydaySignMapper.selectByUserManagerId(userManagerId);
		if(usermanagerSign != null){
			Date sign_time = usermanagerSign.getSign_time();
			long time = sign_time.getTime();
			String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date(currentTimeMillis));
			String srart_time = format +" 00:00:00";
			String end_time = format +" 23:59:59";
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			long today_start_time = f.parse(srart_time).getTime();
			long today_end_time = f.parse(end_time).getTime();
			return time < today_end_time && time > today_start_time ? false : true;
		}else{
			return true;
		}
		
	}

	@Override
	public String addUserManagerIsSing(Long userManagerId) {
		UsermanagerEverydaySign usermanagerEverydaySign = new UsermanagerEverydaySign();
		UsermanagerEverydaySign usermanagerSign = usermanagerEverydaySignMapper.selectByUserManagerId(userManagerId);
		if(usermanagerSign == null){
			usermanagerEverydaySign.setUsermanager_id(userManagerId);
			usermanagerEverydaySign.setSign_time(new Date());
			usermanagerEverydaySignMapper.insertSelective(usermanagerEverydaySign);
		}else{
			usermanagerEverydaySign.setId(usermanagerSign.getId());
			usermanagerEverydaySign.setSign_time(new Date());
			usermanagerEverydaySignMapper.updateByPrimaryKeySelective(usermanagerEverydaySign);
		}
		//导购添加签到积分
		int num = sellerPointEntityMapper.selectIsFirstAddByUserManagerId(userManagerId,6);
		SellerPointEntity sellerPointEntity = new SellerPointEntity();
		sellerPointEntity.setSeller_id(userManagerId);
		sellerPointEntity.setType_index(6);
		sellerPointEntity.setType_name("签到");
		sellerPointEntity.setTitle("签到积分");
		sellerPointEntity.setCreate_time(new Date());
		if(num == 0){
			sellerPointEntity.setPoint(2);
			sellerPointEntityMapper.insertSelective(sellerPointEntity);
			return "2";
		}else{
			sellerPointEntity.setPoint(1);
			sellerPointEntityMapper.insertSelective(sellerPointEntity);
			return "1";
		}
	}

	
	

	

}
